﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		ConfigurationRepository.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 //   File:            ConfigurationRepository.cs
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using System;
using Dexter.Core.Data.Repository;
using Dexter.Domain.Model;
using Dexter.Logger.Contracts;
using Dexter.Repository.NHibernate.SessionFactory;
using Topics.Radical.Validation;

namespace Dexter.Repository.NHibernate.Repositories {
	/// <summary>
	/// The implementation of <see cref="IConfigurationRepository"/>.
	/// </summary>
	/// <example>The following is an example of initializing 
	///          an <c>ConfigurationRepository</c> type:
	///   <code>
	///       // Create the type.
	///       ConfigurationRepository ds = new ConfigurationRepository;
	/// 
	///       if ( null == ds )
	///           return;
	/// 
	///       return ds.DoSomething( 5 );
	///   </code>
	/// </example>
	public class ConfigurationRepository : RepositoryBase <Domain.Model.Configuration> , IConfigurationRepository {
		/// <summary>
		/// Initializes a new instance of the <see cref="ConfigurationRepository"/> class.
		/// </summary>
		/// <param name="sessionFactory">The session factory.</param>
		/// <param name="logger">The logger.</param>
		public ConfigurationRepository ( ISessionFactory sessionFactory , ILogger logger ) : base ( sessionFactory , logger ) {}

		#region IConfigurationRepository Members

		/// <summary>
		/// Adds the specified item.
		/// </summary>
		/// <param name="item">The item.</param>
		public override void Add ( Domain.Model.Configuration item ) {
			this.SaveConfiguration ( item );
		}

		/// <summary>
		/// Saves the specified configuration.
		/// </summary>
		/// <param name="configuration">The configuration.</param>
		/// <exception cref="ArgumentNullException"> If <paramref name="configuration"/> is <c>null</c>.</exception>
		public virtual void SaveConfiguration ( Domain.Model.Configuration configuration ) {
			Ensure.That ( configuration )
				.Named ( "configuration" )
				.WithPreview ( ( v , e ) => Logger.Warn ( v.GetFullErrorMessage ( ) , e ) )
				.IsNotNull ( )
				.WithMessage ( "Invalid configuration" );

			Session.SaveOrUpdate ( configuration );
		}

		/// <summary>
		/// Retrieve the application configuration
		/// </summary>
		/// <returns>
		/// An instance of <see cref="Configuration"/>. It will never be null.
		/// </returns>
		public virtual Domain.Model.Configuration GetConfiguration ( ) {
			Session.SessionFactory.Evict ( typeof ( Domain.Model.Configuration ) );
			
			return Session.Get <Domain.Model.Configuration> ( 1 );
		}

		#endregion
	}
}